/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dreamwar.model;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Berto_Romero
 */
public class PersistenceProperties {

    private static PersistenceProperties persistenceProperties;
    private final String password;
    private final String port;
    private final String ipAddress;
    private final String user;
    
    public static PersistenceProperties getInstance() {
        if (persistenceProperties == null) {
            persistenceProperties = new PersistenceProperties("", "", "", "");
        }
        return persistenceProperties;
    }
    
    public static PersistenceProperties getInstance(String user, String password, String ipAddress, String port) {
        if (persistenceProperties == null || !persistenceProperties.isReady()) {
            persistenceProperties = new PersistenceProperties(user, password, ipAddress, port);
        }
        return persistenceProperties;
    }
    
    public static PersistenceProperties getInstance(String[] customPersistencePropertiesValues) {
        if (persistenceProperties == null || !persistenceProperties.isReady()) {
            persistenceProperties = new PersistenceProperties(customPersistencePropertiesValues);
        }
        return persistenceProperties;
    }
    
    public static PersistenceProperties getInstance(Map<String, String> customPersistencePropertiesValues) {
        if (persistenceProperties == null || !persistenceProperties.isReady()) {
            persistenceProperties = new PersistenceProperties(customPersistencePropertiesValues);
        }
        return persistenceProperties;
    }

    private PersistenceProperties(String user, String password, String ipAddress, String port) {
        this.user = user;
        this.password = password;
        this.ipAddress = ipAddress;
        this.port = port;
    }

    private PersistenceProperties(String[] customPersistencePropertiesValues) {
        this(customPersistencePropertiesValues[0], 
                customPersistencePropertiesValues[1], 
                customPersistencePropertiesValues[2], 
                customPersistencePropertiesValues[3]);
    }

    private PersistenceProperties(Map<String, String> customPersistencePropertiesValues) {
        this(customPersistencePropertiesValues.get("user"), 
                customPersistencePropertiesValues.get("password"), 
                customPersistencePropertiesValues.get("ipAddress"), 
                customPersistencePropertiesValues.get("port"));
    }
    
    public boolean isReady() {
        try {
            DriverManager.getConnection ("jdbc:mysql://" + ipAddress + ":" + port + "/dreamwar_sif", user, password);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(PersistenceProperties.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public String getPassword() {
        return password;
    }

    public String getPort() {
        return port;
    }

    public String getUrl() {
        return ipAddress;
    }

    public String getUser() {
        return user;
    }
    
}
